﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace SportingGoodsExhibitionSystem
{
    /// <summary>
    /// FunctionFour.xaml 的交互逻辑
    /// </summary>
    public partial class FunctionFour : Page
    {
        public FunctionFour()
        {
            InitializeComponent();
        }
        string strCon = Properties.Resources.strCON;
        int stuNum;
        int jug;
        
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from studentInfo where studentStatus=@num", con);
            cmd.Parameters.Add("@num", SqlDbType.Int, 10).Value = 1;
            SqlDataAdapter read = new SqlDataAdapter(cmd);

            DataSet dataset = new DataSet();
            read.Fill(dataset);
            DataTable table = dataset.Tables[0];
            DataRowCollection rows = table.Rows;
            DataRow row = rows[0];
            stuNum = Convert.ToInt32(row["studentNumber"].ToString());
            labelName.Content = row["studentName"].ToString();
            labelNum.Content = stuNum.ToString();
           
            try
            {
                cmd = new SqlCommand("select * from vacation where studentId=@Id and status=@Sta", con);
                cmd.Parameters.Add("@Id", SqlDbType.Int, 10).Value = stuNum;
                cmd.Parameters.Add("@Sta", SqlDbType.Int, 10).Value = 1;
                SqlDataAdapter read1 = new SqlDataAdapter(cmd);
                DataSet dataset1 = new DataSet();
                read1.Fill(dataset1);
                DataTable table1 = dataset1.Tables[0];
                DataRowCollection rows1 = table1.Rows;
                DataRow row1 = rows1[0];
                labelReason.Content = row1["reason"].ToString();
                jug = 1;
            }
            catch
            {
                MessageBox.Show("不在请假中,无需销假");
            }
            con.Close();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(jug==1)
            {
                SqlConnection con = new SqlConnection(strCon);
                con.Open();
                SqlCommand cmd = new SqlCommand("update vacation set status=@st where studentId=@id and status=@sta", con);
                cmd.Parameters.Add("@st", SqlDbType.Int, 10).Value = 0;
                cmd.Parameters.Add("@id", SqlDbType.Int, 10).Value = stuNum;
                cmd.Parameters.Add("@sta", SqlDbType.Int, 10).Value = 1;
                cmd.ExecuteNonQuery();
                cmd = new SqlCommand("update studentInfo set leaveStatus=@st where studentNumber=@id ", con);
                cmd.Parameters.Add("@st", SqlDbType.Int, 10).Value = 0;
                cmd.Parameters.Add("@id", SqlDbType.Int, 10).Value = stuNum;
                cmd.ExecuteNonQuery();
                MessageBox.Show("销假成功");
                labelReason.Content = "";
            }
            else
            {
                MessageBox.Show("不在请假中，无需销假");
            }
        }
    }
}
